package loader.core.load
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	
	import loader.core.BaseServer;
	import loader.core.LoadFile;
	
	
	public class BaseLoader extends EventDispatcher
	{
		public var file:LoadFile;//加载资源
		public var server:BaseServer;//服务器是谁
		public var isLoading:Boolean;//是否加载中
		
		public function BaseLoader(server:BaseServer)
		{
			this.server = server;
		}
		
		public function initListener():void{}
		public function removeListener():void{}
		
		public function loadFile(file:LoadFile):void{
			this.file = file;
			this.isLoading = true;	
			initListener();		
		}
		
		public function completeHandler(event:Event):void{
			this.server.comHandler(file);
			destory();
			loadNext();
		}
		
		public function ioErrorHandler(event:IOErrorEvent):void{
			destory();
			this.server.sendStatus("加载IO错误url="+file.loadUrl);
		}
		
		//清除资源
		public  function destory():void{
			this.isLoading = false;
			removeListener();
		}
		//加载下一个
		public function loadNext():void{
			try{
				if(this.server.fileArray.length > 0){
					var arr:Vector.<LoadFile> = this.server.fileArray.splice(0,1);
					if(arr != null && arr[0] != null){
			 			loadFile(arr[0] as LoadFile);
			 		}
			 	}
		 	}catch(err:Error){
		 		this.server.sendStatus("加载错误url=",err.message);
		 	}
		}
	}
}